<?php
/**
 * 
 * Enter description here ...
 * @param unknown_type $form
 * @param unknown_type $form_state
 * @param unknown_type $form_id
 */
function flexmenu_menu_edit(&$form, &$form_state, $form_id){
  $module_path = drupal_get_path('module', 'flexmenu');
  $menu_path = $module_path . '/menus';
  $files = file_scan_directory($menu_path, '\.ini$');
  
  $otpions = array();
  $options['0'] = 'None';
  $params = array();
  foreach ($files as $path=>$info){
    $params[$info->name] = drupal_parse_info_file($path);
    $options[$info->name] = isset($params[$info->name]['name']) ? $params[$info->name]['name']: $info->name;
  }
  
  $default_value = NULL;
  
  if(isset($form['menu_name']['#value'])){
    $query = "SELECT * FROM {fm_menu} WHERE menu_name = '".$form['menu_name']['#value']."'";
    $result = db_query($query);
    
    while ($row = db_fetch_object($result)){
      $default_value = $row->type;
      $settings = unserialize($row->settings);
    }
  }
  
  //load default value
  $form['menu_type'] = array(
    '#type'=>'select',
    '#title'=>t('Menu type'),
    '#description' => t('You can choose menu type (mega menu, split menu, suffic menu...).<br/> 
                         Each menu have custom parameter.'),
    '#options'=>$options,
    '#default_value' => $default_value
  );
  
  $form['flexmenu'] = array(
    '#tree' => TRUE,
  );
  
  foreach ($params as $name=>$info){
    $form['flexmenu'][$name] = array(
      '#type' => 'fieldset',
      '#title' => isset($info['name']) ? $info['name'] : $name,
      '#attributes' => array('id'=>'flexmenu-'.$name, 'style'=>'display:none;'),
      '#description' => $info['description']
    );
    
    
    flexmenu_fill_default_value($settings, $info['params']);
    $form['flexmenu'][$name] += $info['params'];
    //need to add defaul value to here
  }
  
  drupal_add_js($module_path.'/flexmenu.js');
  //need to using array_multi_sort
  $backup_submit_button = $form['submit'];
  unset($form['submit']);
  $form['submit'] = $backup_submit_button;
  
  $form['#submit'] = array_merge($form['#submit'],  array('flexmenu_menu_save'));
  //Todo : need to add delete function 
}
/**
 * 
 * Enter description here ...
 * @param unknown_type $form
 * @param unknown_type $form_state
 */
function flexmenu_menu_save($form, &$form_state){
  $values = $form_state['values'];
  
  $menu_name = $values['menu_name'];
  $type = $values['menu_type'];
  
  if($type == '0'){
    $query = "DELETE FROM {fm_menu} WHERE menu_name = '$menu_name'";
    $result = db_query($query);
  }else {
    
    $query = "SELECT id FROM {fm_menu} WHERE menu_name = '$menu_name'";
    $result = db_query($query);
    
    $exits = FALSE;
    while ($row = db_result($result)){
      $exits = TRUE;
    }
    
    
    //now we can do update or insert with $exites variable
    if($exits){
      $query = "UPDATE {fm_menu} SET type = '$type', settings = '%s' WHERE menu_name = '$menu_name'" ;
      db_query($query, array(serialize($values['flexmenu'][$type])));
    }else{
      $query = "INSERT INTO {fm_menu}(`menu_name`, `type`, `settings`) VALUES ('%s', '%s', '%s')";
      db_query($query, array($menu_name, $type, serialize($values['flexmenu'][$type])));
    }
  }
  
  cache_clear_all('flexmenu', 'cache_menu');
}

/**
 * 
 * Enter description here ...
 * @param unknown_type $form
 * @param unknown_type $form_state
 * @param unknown_type $form_id
 */
function flexmenu_menu_item_edit(&$form, &$form_state, $form_id){
  $item = $form['menu']['original_item']['#value'];
  
  $name = $item['menu_name'];
  
  //select menutype from database
  $query = "SELECT type FROM {fm_menu} WHERE menu_name ='$name'";
  $result = db_query($query);
  
  
  $type = FALSE;
  while ($row = db_fetch_object($result)){
    $type = $row->type;
  }
  
  if(!$type) return;
  
  //get config item here
  if(isset($item['mlid'])){
    $query = "SELECT * FROM {fm_menu_item} WHERE menu_name = '".$item['menu_name']."' AND mlid = '".$item['mlid']."'";
    $result = db_query($query);
    while ($row = db_fetch_object($result)){
      $settings = unserialize($row->settings);
      
    }
  }
  
  //get menu item params
  $module_path = drupal_get_path('module', 'flexmenu');
  $config_file = $module_path.'/menus/'.$type.'.ini';
  
  $params = drupal_parse_info_file($config_file);
  $form['flexmenu'] = array(
    '#type'=>'fieldset',
    '#title'=>t('@name configuration', array('@name' => $params['name'])),
    '#weight'=> 0,
    '#tree' => TRUE,
  );
  
  flexmenu_fill_default_value($settings, $params['items']);
  
  $form['flexmenu'] += $params['items'];
  $form['#submit'] = array_merge($form['#submit'],  array('flexmenu_menu_item_save'));
}

/**
 * 
 * Enter description here ...
 * @param unknown_type $form
 * @param unknown_type $form_state
 */
function flexmenu_menu_item_save($form, &$form_state){
  $values = $form_state['values'];
  
  if(!isset($values['flexmenu'])){
    return;
  }
  
  $settings = $values['flexmenu'];
  
  $menu_name = $values['menu']['menu_name'];
  $mlid = $values['menu']['mlid'];
  
  $query = "SELECT id FROM {fm_menu_item} WHERE menu_name = '$menu_name' AND mlid = '$mlid'";
  $result = db_query($query);
  
  $exits = FALSE;
  while ($row = db_result($result)){
    $exits = TRUE;
  }
  
  
  //now we can do update or insert with $exites variable
  if($exits){
    $query = "UPDATE {fm_menu_item} SET menu_name = '$menu_name', settings = '%s' WHERE mlid = '$mlid'";
    db_query($query, array(serialize($settings)));
  }else{
    $query = "INSERT INTO {fm_menu_item}(`menu_name`, `mlid`, `settings`) VALUES ('%s', '%s', '%s')";
    db_query($query, array($menu_name, $mlid, serialize($settings)));
  }
  
  cache_clear_all('flexmenu', 'cache_menu');
}




function flexmenu_fill_default_value($array, &$params){
  if(!is_array($array)) return;
  foreach ($array as $key=>$value){
    if(!isset($params[$key]['#type'])){
      $sub_params =& $params[$key];
      flexmenu_fill_default_value($value, $sub_params);
    }else{
      $params[$key]['#default_value'] = $value;
    }
  }
}
